﻿using Blm.Utils.Extensions;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;
using VisionCloud.Domain.UnitOfWork;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsTaskWxMessageRepository : BaseRepository<MsTaskWxMessage>, IMsTaskWxMessageRepository
    {
        public IUnitOfWork UnitOfWork { get; set; }

        public async ValueTask<bool> AddMessageAsync(List<MsTaskWxMessage> messageList)
        {
            var ids = messageList.Select(x => x.TaskId).ToList();

            var list = await Db.MsTaskWxMessage.Where(p => ids.Contains(p.TaskId) && !p.IsDeleted && p.Status == 0).ToListAsync();
            if (list.IsNotEmpty())
            {
                foreach (var item in list)
                {
                    item.IsDeleted = true;
                    item.LastUpdatedDate = DateTime.Now;
                }
                Db.MsTaskWxMessage.UpdateRange(list);
            }
            Db.MsTaskWxMessage.AddRange(messageList);

            var affrows = await UnitOfWork.SaveChangesAsync();

            return affrows > 0;
        }

        public async ValueTask<bool> UpdateRangeMessageAsync(List<MsTaskWxMessage> messageList)
        {
            messageList.ForEach(p =>
            {
                p.Status = 1;
                p.LastUpdatedDate = DateTime.Now;
            });

            Db.MsTaskWxMessage.UpdateRange(messageList);

            var affrows = await UnitOfWork.SaveChangesAsync();

            return affrows > 0;
        }
    }
}
